package com.stu_ques.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import com.stu_ques.common.util.DateUtil;
import com.stu_ques.demo.dao.IDynastyDao;
import com.stu_ques.demo.dao.IKingDao;
import com.stu_ques.demo.dao.impl.DynastyDaoImpl;
import com.stu_ques.demo.dao.impl.KingDaoImpl;
import com.stu_ques.demo.pojo.Dynasty;
import com.stu_ques.demo.pojo.King;
import com.stu_ques.demo.service.IDemoService;
import com.stu_ques.demo.service.impl.DemoServiceImpl;
import com.stu_ques.question.dao.IAnswerDao;
import com.stu_ques.question.dao.IQuestionDao;
import com.stu_ques.question.dao.impl.AnswerDaoImpl;
import com.stu_ques.question.dao.impl.QuestionDaoImpl;
import com.stu_ques.question.pojo.Answer;
import com.stu_ques.question.pojo.Question;
import com.stu_ques.question.service.IQuestionService;
import com.stu_ques.question.service.impl.QuestionServiceImpl;
import com.stu_ques.users.dao.ILevelDao;
import com.stu_ques.users.dao.IStudentDao;
import com.stu_ques.users.dao.impl.LevelDaoImpl;
import com.stu_ques.users.dao.impl.StudentDaoImpl;
import com.stu_ques.users.pojo.Level;
import com.stu_ques.users.pojo.Student;
import com.stu_ques.users.service.IUsersService;
import com.stu_ques.users.service.impl.UsersServiceImpl;

public class ClientMain
{
	/**
	 * 日期的工具类
	 */
	private DateUtil dateUtil = new DateUtil();
	/**
	 * 引进Service
	 */
	private IDemoService demoService;
	private IQuestionService questionService;
	private IUsersService usersService;
	/**
	 *	 启动main方法
	 */
	public static void main(String[] args)
	{
		System.out.println("=程序开始执行=");
		ClientMain clientMain = new ClientMain();
		
		IUsersService usersService = new UsersServiceImpl();
		if(usersService instanceof UsersServiceImpl)
		{
			UsersServiceImpl usersServiceImpl = (UsersServiceImpl) usersService;
			IStudentDao studentDao = new StudentDaoImpl();
			ILevelDao levelDao = new LevelDaoImpl();
			
			usersServiceImpl.setLevelDao(levelDao);
			usersServiceImpl.setStudentDao(studentDao);
		}
		clientMain.setUsersService(usersService);
		
		IQuestionService questionService = new QuestionServiceImpl();
		if(questionService instanceof QuestionServiceImpl)
		{
			QuestionServiceImpl questionServiceImpl = (QuestionServiceImpl) questionService;
			IQuestionDao questionDao = new QuestionDaoImpl();
			IAnswerDao answerDao = new AnswerDaoImpl();
			questionServiceImpl.setAnswerDao(answerDao);
			questionServiceImpl.setQuestionDao(questionDao);
			questionServiceImpl.setUsersService(usersService);
		}
		clientMain.setQuestionService(questionService);
		
		IDemoService demoService = new DemoServiceImpl();
		if(demoService instanceof DemoServiceImpl)
		{
			DemoServiceImpl demoServiceImpl = (DemoServiceImpl) demoService;
			IDynastyDao dynastyDao = new DynastyDaoImpl();
			IKingDao kingDao = new KingDaoImpl();
			
			demoServiceImpl.setKingDao(kingDao);
			demoServiceImpl.setDynastyDao(dynastyDao);
		}
		clientMain.setDemoService(demoService);
		clientMain.lanuch();
	}

	private void lanuch()
	{
		System.out.println("=欢迎使用本系统,按下h(help)可以查看帮助;=");
		BufferedReader br = null;
		try
		{
			/* 把管子接到键盘上 */
			br = new BufferedReader(new InputStreamReader(System.in, "utf-8"));
			/* 用户输入的内容 */
			String line = "";
			while (true)
			{
				System.out.println("=主菜单:=按下n停止=");
				line = br.readLine();
				line.trim();
				if ("n".equalsIgnoreCase(line))
				{
					System.out.println("=欢迎下次使用=");
					break;
				} else if ("h".equalsIgnoreCase(line))
				{
					/* 菜单 */
					help();
				} else if ("1".equalsIgnoreCase(line))
				{
					/* 查询所有朝代 */
					this.selectListDynaty();
				} else if ("2".equalsIgnoreCase(line))
				{
					/* 添加一个朝代 */
					this.saveDynaty(br);
				} else if ("3".equalsIgnoreCase(line))
				{
					/* 修改一个朝代 */
					this.updateDynaty(br);
				} else if ("4".equalsIgnoreCase(line))
				{
					/* 删除一个朝代 */
					this.deleteDynaty(br);
				} else if ("5".equalsIgnoreCase(line))
				{
					/* 查询单个朝代 */
					this.selecOneDynaty(br);
				} else if ("6".equalsIgnoreCase(line))
				{
					/* 查询所有皇上 */
					this.selectListKing();
				} else if ("7".equalsIgnoreCase(line))
				{
					/* 添加皇上 */
					this.saveOneKing(br);
				} else if ("8".equalsIgnoreCase(line))
				{
					/* 修改皇上 */
					this.updateKing(br);
				} else if ("9".equalsIgnoreCase(line))
				{
					/* 删除皇上 */
				} else if ("10".equalsIgnoreCase(line))
				{
					/* 查询单个皇上 */
					this.selecOneKing(br);
				} else if ("11".equalsIgnoreCase(line))
				{
					/* 查询所有学生 */
					this.selectListStudent();
				} else if ("12".equalsIgnoreCase(line))
				{
					/* 添加一个学生 */
					this.saveOneStudent(br);
				} else if ("13".equalsIgnoreCase(line))
				{
					/* 修改一个学生 */
					this.updateOneStudent(br);
				} else if ("14".equalsIgnoreCase(line))
				{
					/* 删除一个学生 */
					this.deleteOneStudent(br);
				} else if ("15".equalsIgnoreCase(line))
				{
					/* 查询单个学生 */
					this.selectOneStudent(br);
				} else if ("16".equalsIgnoreCase(line))
				{
					/* 查询所有学生 */
					this.selectListLevel();
				} else if ("17".equalsIgnoreCase(line))
				{
					/* 添加等级 */
					this.saveOneLevel(br);
				} else if ("18".equalsIgnoreCase(line))
				{
					/* 修改等级 */
					this.updateOneLevelService(br);
				} else if ("19".equalsIgnoreCase(line))
				{
					/* 删除等级 */
					this.deleteOneLevel(br);
				} else if ("20".equalsIgnoreCase(line))
				{
					/* 查询单个等级 */
					this.selectOneLevel(br);
				} else if ("21".equalsIgnoreCase(line))
				{
					/* 查询所有的问题 */
					this.selectListQuestionService();
				} else if ("22".equalsIgnoreCase(line))
				{
					/* 添加问题 */
					this.saveOneQuestionService(br);
				} else if ("23".equalsIgnoreCase(line))
				{
					/* 更新问题 */
					this.updateOneQuestionService(br);
				} else if ("24".equalsIgnoreCase(line))
				{
					/* 删除问题 */
					this.deleteOneQuestionService(br);
				} else if ("25".equalsIgnoreCase(line))
				{
					/* 查询所有的问题 */
					this.selectOneQuestionService(br);
				} else if ("26".equalsIgnoreCase(line))
				{
					/* 查找所有答案 */
					this.selectListAnswerService(br);
				} else if ("27".equalsIgnoreCase(line))
				{
					/* 添加一个答案 */
					this.saveOneAnswerService(br);
				} else if ("28".equalsIgnoreCase(line))
				{
					/* 更新一个答案 */
					this.updateOneAnswerService(br);
				} else if ("29".equalsIgnoreCase(line))
				{
					/* 删除一个答案 */
					this.deleteOneAnswerService(br);
				} else if ("30".equalsIgnoreCase(line))
				{
					/* 查找一个答案 */
					this.selectOneAnswerService(br);
				} else if ("50".equalsIgnoreCase(line))
				{
					this.randomAsk(br);
				}
			}
		} catch (UnsupportedEncodingException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * 随机数
	 */
	private void randomAsk(BufferedReader br)
	{
		try
		{
			/* 根据id查寻问题 */
			Question question = this.selectOneQuestionService(br);
			if (question != null)
			{
				while (true)
				{
					/*
					 * 查询问题,抽学生
					 * 	用学生ID抽奖
					 */
					int stuMaxId = this.usersService.selectStudetMaxIdService();
					Random random = new Random();
					Student student = null;
					/* 计数器 */
					int count = 0;
					while (true)
					{
						int tempId = random.nextInt(stuMaxId);
						/* 根据学生的id 查找学生 */
						Map<String, Object> condMap = new HashMap<String, Object>();
						condMap.put("id", tempId);
						student = this.usersService.selectStudentService(condMap);
						/* 如果抽中了 */
						if (student != null)
						{
							/* 放到循环中如果没抽中 接着抽 */
							break;
						}
						count++;
					}
					/* 抽出学生之后在抽一个问提 */
					if (student != null)
					{
						/* 打印问题 */
						System.out.println(stuMaxId + "==起来回答问题==");
						System.out.println(student);
						/* 等待学生回答问题 答案做好保存 */
						System.out.println("==亲请输入答案的相关信息：（答案;对错)");
						System.out.println("如:5;对");
						String line = br.readLine();
						line = line.trim();
						/* 拆分字符串 */
						String[] lines = line.split(";");
						if (lines.length != 2)
						{
							System.out.println("=您输入的格式有误=");
						}
						/* 将用户的内容赋值给答案 */
						Answer answer = new Answer();
						answer.setAnswer(lines[0]);
						answer.setRightAndWrong(lines[1]);
						answer.setCreationTime(new Date());
						answer.setUpdateTime(new Date());
						/* 关联字段 */
						answer.setQuestionId(question.getId());
						answer.setStudentId(student.getId());
						/* 保存答案 */
						int res = this.questionService.saveOneAnswerService(answer);
						System.out.println("==保存成功==" + res);
						if ("对".equalsIgnoreCase(answer.getRightAndWrong()))
						{
							/* 回答正确 */
							System.out.println("=此次回答over=");
							break;
						}
					}
				}
			}
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * 查询一个问题
	 */
	private Answer selectOneAnswerService(BufferedReader br)
	{
		System.out.println("--查询单个答案，请输入id--");

		try
		{
			String id = br.readLine();
			Map<String, Object> condMap = new HashMap<String, Object>();

			condMap.put("id", Integer.valueOf(id));
			Answer answer = this.questionService.selectAnswerService(condMap);
			if (answer == null)
			{
				System.out.println("==您输入的id，找不到答案==");
			} else
			{
				String title = questionService.printTitleAnswerService();
				System.out.println(title);
				System.out.println(answer);

			}
			return answer;
		} catch (NumberFormatException e)
		{
			// e.printStackTrace();
			System.out.println("==您输入的ID应该为整数==");
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		return null;

	}

	/**
	 * 删除一个问题
	 */
	private void deleteOneAnswerService(BufferedReader br)
	{
		Answer answer = this.selectOneAnswerService(br);
		if (answer != null)
		{
			this.questionService.deleteOneAnswerService(answer);

			System.out.println("==删除成功==id：" + answer.getId());
		} else
		{
			System.out.println("此条记录不存在");
		}

	}

	/**
	 * 更新一个问题
	 */
	private void updateOneAnswerService(BufferedReader br)
	{
		Answer answer = this.selectOneAnswerService(br);
		if (answer != null)
		{
			this.questionService.deleteOneAnswerService(answer);

			System.out.println("==删除成功==id：" + answer.getId());
		} else
		{
			System.out.println("此条记录不存在");
		}

	}

	/**
	 * 添加一个问题
	 */
	private void saveOneAnswerService(BufferedReader br)
	{

		System.out.println("==亲请输入答案的相关信息：（学生id;答案;是否正确");
		System.out.println("示例：22;5;正确");

		try
		{
			String line = br.readLine();
			line = line.trim();
			String[] lines = line.split(";");

			if (lines.length != 3)
			{
				System.out.println("==输入的格式不正确==");
				return;
			}

			Answer answer = new Answer();
			answer.setStudentId(Integer.valueOf(lines[0]));
			answer.setRightAndWrong(lines[2]);
			answer.setAnswer(lines[3]);

			answer.setCreationTime(new Date());
			answer.setUpdateTime(new Date());
			int id = this.questionService.saveOneAnswerService(answer);

			System.out.println("==保存成功，id：==" + id);
		} catch (NumberFormatException e)
		{
			// e.printStackTrace();
			System.out.println("==您输入的字符串变不成数字");
		} catch (IOException e)
		{
			e.printStackTrace();
		}

	}

	/**
	 * 查询所有答案
	 */
	private void selectListAnswerService(BufferedReader br)
	{
		List<Answer> AnswerList = questionService.selectListAnswerService();

		String title = questionService.printTitleAnswerService();
		System.out.println(title);
		for (Answer answer1 : AnswerList)
		{
			System.out.println(answer1);
		}

	}

	/**
	 * 查询一个问题
	 */
	private Question selectOneQuestionService(BufferedReader br)
	{
		System.out.println("---查询单个问题，请输入--");

		try
		{
			String id = br.readLine();
			id = id.trim();
			/* 查询的条件 */
			Map<String, Object> condMap = new HashMap<String, Object>();
			/*
			 * 根据id查询,因为id是Map的键,通过键找值,特别快. Map是在Dao使用的,由于在Dao中get(id);键在dao中已经定死了,就是id
			 */
			condMap.put("id", Integer.valueOf(id));
			Question question = questionService.selectQuestionService(condMap);
			/* 标题 */
			if (question == null)
			{
				System.out.println("您输入的Id，找不到问题==");
			} else
			{
				String title = this.questionService.printTitleQuestionService();
				System.out.println(title);
				System.out.println(question);
			}

			return question;

		} catch (IOException e)
		{
			e.printStackTrace();
		} catch (Exception e)
		{
			e.printStackTrace();
		}
		return null;

	}

	/**
	 * 删除一个问题
	 */
	private void deleteOneQuestionService(BufferedReader br)
	{
		Question question = this.selectOneQuestionService(br);
		if (question != null)
		{
			questionService.deleteOneQuestionService(question);
			System.out.println("===删除成功==id：" + question.getId());
		} else
		{
			System.out.println("==此条记录不存在==");
		}

	}

	/**
	 * 更新一个问题
	 */

	private void updateOneQuestionService(BufferedReader br)
	{
		Question question = selectOneQuestionService(br);
		if (question == null)
		{
			return;
		}
		String templateStr = "更新的示例" + question.getId() + question.getName() + "|" + question.getScore();
		System.out.println(templateStr);

		try
		{
			String line = br.readLine();
			line = line.trim();
			String[] lines = line.split("\\|");

			if (lines.length != 3)
			{
				System.out.println("==输入的格式不正确");
				return;
			}
			question.setId(Integer.valueOf(lines[0]));
			question.setName(lines[1]);
			question.setScore(Integer.valueOf(lines[2]));
			question.setUpdateTime(new Date());

			questionService.updateOneQuestionService(question);
			System.out.println("==更新成功，ID：==" + question.getId());
		} catch (NumberFormatException e)
		{
			// e.printStackTrace();
			System.out.println("==您输入的字符串变不成数字==");
		} catch (IOException e)
		{
			e.printStackTrace();
		}

	}

	/**
	 * 
	 * 添加一个问题
	 */
	private void saveOneQuestionService(BufferedReader br)
	{
		System.out.println("===请输入问题的相关信息：（名字;分数）");
		System.out.println("示例：加法;20");

		try
		{
			String line = br.readLine();
			line = line.trim();

			String[] lines = line.split(";");

			if (lines.length != 2)
			{

				System.out.println("输入的格式不正确");
				return;
			}

			Question question = new Question();
			// question.setId(Integer.valueOf(lines[0]));
			question.setName(lines[0]);
			question.setScore(Integer.valueOf(lines[1]));
			question.setCreationTime(new Date());
			question.setUpdateTime(new Date());

			int id = questionService.saveOneQuestionService(question);
			System.out.println("===保存成功，id：==" + id);
		} catch (NumberFormatException e)
		{
			// e.printStackTrace();
			System.out.println("您输入的字符串变不成数字");
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * 查找所有问题
	 */
	private void selectListQuestionService()
	{
		List<Question> questionList = questionService.selectListQuestionService();

		String title = questionService.printTitleQuestionService();
		System.out.println(title);
		for (Question question : questionList)
		{
			System.out.println(question.getId() + "\t" + question.getName() + "\t" + question.getScore() + "\t"
					+ question.getScore() + "\t" + question.getCreationTime() + "\t" + question.getUpdateTime());

		}

	}

	/**
	 * 更新一个皇上
	 */
	private void updateKing(BufferedReader br)
	{
		King king = this.selecOneKing(br);
		if (king == null)
		{
			return;
		}
		/* 要把原来的朝代内容列出来;(列的是模板字符串) */
		String templateStr = "更新的示例:" + king.getName() + ";" + king.getAge() + ";" + king.getMianHao() + ";"
				+ king.getNianHao();
		System.out.println(templateStr);
		try
		{
			/* 连接键盘输入流 */
			String line = br.readLine();
			line = line.trim();
			/* 拆分字符串赋值 */
			String[] lines = line.split("\\|");
			if (lines.length != 4)
			{
				/* 输入的格式不正确 */
				System.out.println("==输入的格式不正确==");
				return;
			}
			king.setName(lines[0]);
			king.setAge(Short.valueOf(lines[1]));
			king.setNianHao(lines[3]);
			king.setMianHao(lines[4]);
			king.setUpdateThim(new Date());
			this.demoService.updateOneDemoKingService(king);
			System.out.println("=更新成功=");
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * 添加一个皇上
	 */
	private void saveOneKing(BufferedReader br)
	{
		System.out.println("=请输入皇上相关系信息:(名字;年龄;年号;庙号)=");
		System.out.println("=如:朱元璋;65;洪武;明太祖=");
		try
		{
			/* 连接键盘输入流 */
			String line = br.readLine();
			line = line.trim();
			/* 拆分字符串 */
			String[] lines = line.split(";");
			if (lines.length != 5)
			{
				System.out.println("=您输入的格式不正确=");
				return;
			}
			King king = new King();
			king.setDynastyId(Integer.valueOf(lines[0]));
			king.setName(lines[1]);
			king.setAge(Short.valueOf(lines[2]));
			king.setNianHao(lines[3]);
			king.setMianHao(lines[4]);
			king.setCreateTime(new Date());
			king.setUpdateThim(new Date());
			int id = this.demoService.saveOneDemoKingService(king);
			System.out.println("=添加成功=" + id);
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * 查询单个皇上
	 */
	private King selecOneKing(BufferedReader br)
	{
		/* 告诉用户怎么查询 */
		System.out.println("=请输入id=");
		try
		{
			/* 连接键盘输入流 */
			String line = br.readLine();
			line = line.trim();
			/* 查询条件 */
			Map<String, Object> condMap = new HashMap<String, Object>();
			King king = this.demoService.selectDemoKingService(condMap);
			if (king == null)
			{
				System.out.println("=您输入的id没有对应的皇上=");
			} else
			{
				/* 打印标题 */
				System.out.println(this.demoService.printTitleKingService());
				System.out.println(king);
			}
			return king;
		} catch (IOException e)
		{
			e.printStackTrace();
		} catch (NumberFormatException e)
		{
			System.out.println("=你输入的id必须为整数=");
		}

		return null;
	}

	/**
	 * 查询所有皇上
	 */
	private void selectListKing()
	{
		List<King> kingList = this.demoService.selectListDemoKingService();
		/* 打印标题 */
		System.out.println(this.demoService.printTitleKingService());
		for (King king : kingList)
		{
			Dynasty dynasty = king.getDynasty();
			String dynastyName = "空";
			if (dynasty != null)
			{
				dynastyName = dynasty.getName();
			}
			System.out.println(king.getId() + "\t" + dynastyName + "\t" + king.getName() + "\t" + king.getAge() + "\t"
					+ king.getHeijht() + "\t" + king.getMianHao() + "\t" + king.getNianHao() + "\t"
					+ dateUtil.dateTimeString(king.getCreateTime()) + "\t"
					+ dateUtil.dateTimeString(king.getUpdateTime()));
		}
	}

	/**
	 * 删除一个朝代
	 */
	private void deleteDynaty(BufferedReader br)
	{
		/* 根据id查询 */
		Dynasty dynasty = this.selecOneDynaty(br);
		if (dynasty != null)
		{
			/* 调用删除方法 */
			this.demoService.deleteOneDemoService(dynasty);
			System.out.println("==删除成功==id:" + dynasty.getId());
		} else
		{
			System.out.println("==此条记录不存在==");
		}
	}

	/**
	 * 查询一个朝代
	 */
	private Dynasty selecOneDynaty(BufferedReader br)
	{
		/* 告诉用户用Id查询朝代 */
		System.out.println("=请输入Id=");
		try
		{
			/* 连接键盘输入流 */
			String line = br.readLine();
			/* 取出前后空格 */
			line = line.trim();
			/* 查询的条件 */
			Map<String, Object> condMap = new HashMap<String, Object>();
			condMap.put("id", Integer.valueOf(line));
			Dynasty dynasty = this.demoService.selectDemoService(condMap);
			if (dynasty == null)
			{
				System.out.println("=没有此Id 请确认后在输入=");
			} else
			{
				/* 打印标题 */
				System.out.println(this.demoService.printTitleDynastyService());
				System.out.println(dynasty);
			}
			return dynasty;
		} catch (IOException e)
		{
			e.printStackTrace();
		} catch (NumberFormatException e)
		{
			System.out.println("=请您输入整数Id=");
		}
		return null;
	}

	/**
	 * 更新一个朝代
	 */
	private void updateDynaty(BufferedReader br)
	{
		/* 根据ID查询朝代 */
		Dynasty dynasty = this.selecOneDynaty(br);
		if (dynasty == null)
		{
			return;
		}
		/* 填写跟新内容 */
		String templateStr = "更新的示例:" + dynasty.getName() + ";" + dynasty.getAge() + ";" + dynasty.getCapital() + ";"
				+ this.dateUtil.dateTimeString(dynasty.getSt());
		System.out.println(templateStr);
		try
		{
			/* 连接键盘输入流 */
			String line = br.readLine();
			line = line.trim();
			String[] lines = line.split(";");
			if (lines.length != 4)
			{
				System.out.println("=输入的ID格式不正确=");
				return;
			}
			/* 要为属性赋值 */
			dynasty.setName(lines[0]);
			dynasty.setAge(Short.valueOf(lines[1]));
			dynasty.setCapital(lines[2]);
			/* 如何把Calendar变成Date */
			dynasty.setSt(dateUtil.stringToDate(lines[3]));
			dynasty.setUpdateTime(new Date());
			this.demoService.updateOneDemoService(dynasty);
			System.out.println("=更新成功=" + dynasty.getId());
		} catch (IOException e)
		{
			e.printStackTrace();
		} catch (NumberFormatException e)
		{
			// e.printStackTrace();
			System.out.println("==您输入的格式不正确请确认后在输入==");
		}
	}

	/**
	 * 添加一个朝代
	 */
	private void saveDynaty(BufferedReader br)
	{
		/* 告诉用户填写那些信息 */
		System.out.println("=请输入朝代的相关信息:(名字;年龄;首都;开国时间)=");
		/* 写出个实例 */
		System.out.println("=如:=明朝;279;北京;1368-01-01=");
		/* 调用键盘输入流 */
		try
		{
			String line = br.readLine();
			/* 去掉前后空格 */
			line = line.trim();
			/* 拆分字符串 */
			String[] lines = line.split(";");
			/* 判断数组的长度 如果不够就不在执行 */
			if (lines.length != 4)
			{
				System.out.println("=输入的格式不正确=");
				/* 程序不在往下执行 */
				return;
			}
			/* 为拆分的属性赋值 */
			Dynasty dynasty = new Dynasty();
			dynasty.setName(lines[0]);
			dynasty.setAge(Short.valueOf(lines[1]));
			dynasty.setCapital(lines[2]);
			/* 开国时间是一个字符串 需要转变 */
			dynasty.setSt(dateUtil.stringToDate(lines[3]));
			/* 调用保存方法 保存朝代信息 */
			dynasty.setCreateTime(new Date());
			dynasty.setUpdateTime(new Date());
			int id = this.demoService.saveOneDemoService(dynasty);
			System.out.println("=保存成功=" + id);
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * 查询所有朝代 *
	 */
	private void selectListDynaty()
	{
		List<Dynasty> dynastuList = this.demoService.selectListDemoService();
		/* 打印朝代的标题 */
		System.out.println(this.demoService.printTitleDynastyService());
		for (Dynasty dynasty : dynastuList)
		{
			System.out.println(dynasty);
		}
	}

	/**
	 * 主菜单
	 */
	private void help()
	{
		System.out.println("1:查询所有朝代");
		System.out.println("2:添加朝代");
		System.out.println("3:修改朝代;(先输入朝代id,填写要修改的内容)");
		System.out.println("4:删除朝代");
		System.out.println("5:查看单个朝代");
		System.out.println("6:查询所有皇上");
		System.out.println("7:添加皇上");
		System.out.println("8:修改皇上;(先输入皇上id,填写要修改的内容)");
		System.out.println("9:删除皇上");
		System.out.println("10:查询单个皇上");

		System.out.println("11:查询所有学生");
		System.out.println("12:添加学生");
		System.out.println("13:修改学生;(先输入学生id,填写要修改的内容)");
		System.out.println("14:删除学生");
		System.out.println("15:查看单个学生");
		System.out.println("16:查询所有等级");
		System.out.println("17:添加等级");
		System.out.println("18:修改等级;(先输入等级id,填写要修改的内容)");
		System.out.println("19:删除等级");
		System.out.println("20:查询单个等级");

		System.out.println("21:查询所有问题");
		System.out.println("22:添加问题");
		System.out.println("23:修改问题;(先输入问题id,填写要修改的内容)");
		System.out.println("24:删除问题");
		System.out.println("25:查看单个问题");
		System.out.println("26:查询所有答案");
		System.out.println("27:添加答案");
		System.out.println("28:修改答案;(先输入答案id,填写要修改的内容)");
		System.out.println("29:删除答案");
		System.out.println("30:查询单个答案");

		System.out.println("50:随机点名提问");
	}

	/**
	 * 更新一个等级
	 */
	private void updateOneLevelService(BufferedReader br)
	{
		Level level = this.selectOneLevel(br);
		try
		{
			if (level != null)
			{
				System.out.println("请依次输入等级的相关信息:名字,初始分数,结束分数");
				System.out.println("示例:铂金;76;80");
				String line = br.readLine();
				line = line.trim();
				String[] lines = line.split(";");
				if (lines.length != 3)
				{
					System.out.println("你输入的格式有问题");
					return;
				}
				level.setName(lines[0]);
				level.setStartScore(Integer.valueOf(lines[1]));
				level.setEndScore(Integer.valueOf(lines[2]));

				level.setUpdateTime(new Date());
				System.out.println("成功修改了,id:" + level.getId());
			} else
			{
				return;
			}
		} catch (NumberFormatException e)
		{
			System.out.println("你输入的不是整数,请重新输入");
			this.updateOneStudent(br);
		} catch (IOException e)
		{
			e.printStackTrace();
		}

	}

	/**
	 * 删除一个等级
	 */
	private void deleteOneLevel(BufferedReader br)
	{
		Level level = this.selectOneLevel(br);
		if (level != null)
		{
			this.usersService.deleteOneLevelService(level);
			System.out.println("你已经删除了一条,id:" + level.getId());
		} else
		{
			return;
		}

	}

	/**
	 * 查找一个等级
	 */
	private Level selectOneLevel(BufferedReader br)
	{
		System.out.println("请输入你要查询的id");
		try
		{
			String line = br.readLine();
			line = line.trim();
			Map<String, Object> condMap = new HashMap<String, Object>();
			condMap.put("id", Integer.valueOf(line.trim()));
			Level level = this.usersService.selectLevelService(condMap);
			if (level != null)
			{
				System.out.println(this.usersService.printTitleLevelService());
				System.out.println(level);
			} else
			{
				System.out.println("无此记录，查询失败");
			}
			return level;
		} catch (NumberFormatException e)
		{
			System.out.println("你输入的不是整数,请重新输入");
			this.selectOneStudent(br);
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 添加一个等级
	 */
	private void saveOneLevel(BufferedReader br)
	{
		System.out.println("请依次输入等级的相关信息:名字,初始分数,结束分数");
		System.out.println("示例:铂金;76;80");
		try
		{
			String line = br.readLine();
			line = line.trim();
			String[] lines = line.split(";");
			if (lines.length != 3)
			{
				System.out.println("你输入的格式有问题");
				return;
			}
			Level level = new Level();
			level.setName(lines[0]);
			level.setStartScore(Integer.valueOf(lines[1]));
			level.setEndScore(Integer.valueOf(lines[2]));
			level.setUpdateTime(new Date());
			level.setCreationTime(new Date());
			int res = this.usersService.saveOneLevelService(level);
			System.out.println("添加成功,id:" + res);
		} catch (NumberFormatException e)
		{
			System.out.println("你输入的不是整数,请重新输入");
			this.saveOneStudent(br);
		} catch (IOException e)
		{
			e.printStackTrace();
		}

	}

	/**
	 * 查找所有等级
	 */
	private void selectListLevel()
	{
		List<Level> list = usersService.selectListLevelService();
		/* 打印标题 */
		System.out.println(usersService.printTitleLevelService());
		for (Level level : list)
		{
			System.out.println(level);
		}
	}

	/**
	 * 查找一个学生
	 */
	private Student selectOneStudent(BufferedReader br)
	{
		System.out.println("请输入你要查询的id");
		try
		{
			String line = br.readLine();
			line = line.trim();
			Map<String, Object> condMap = new HashMap<String, Object>();
			condMap.put("id", Integer.valueOf(line));
			Student students = this.usersService.selectStudentService(condMap);
			if (students != null)
			{
				System.out.println(this.usersService.printTitleStudentService());
				System.out.println(students);
			} else
			{
				System.out.println("无此记录，查询失败");
			}
			return students;
		} catch (NumberFormatException e)
		{
			System.out.println("你输入的不是整数,请重新输入");
			this.selectOneStudent(br);
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 删除一个学生
	 * 
	 * @param br
	 */
	private void deleteOneStudent(BufferedReader br)
	{
		Student students = this.selectOneStudent(br);
		if (students != null)
		{
			this.usersService.deleteOneStudentService(students);
			System.out.println("你已经删除了一条,id:" + students.getId());
		} else
		{
			return;
		}

	}

	/**
	 * 更新一个学生
	 * 
	 * @param br
	 */
	private void updateOneStudent(BufferedReader br)
	{
		Student students = this.selectOneStudent(br);
		try
		{
			if (students != null)
			{
				System.out.println("请依次输入学生的相关信息:姓名,性别,生日,毕业院校,分数,答对次数,答错次数");
				System.out.println("示例:张三;男;1996-12-03;北大;30;3;4");
				String line = br.readLine();
				line = line.trim();
				String[] lines = line.split(";");
				if (lines.length != 7)
				{
					System.out.println("你输入的格式有问题");
					return;
				}
				students.setName(lines[0]);
				if (lines[1].equals("男"))
				{
					students.setSex(true);
				} else
				{
					students.setSex(false);
				}
				students.setUpdateTime(new Date());
				System.out.println("成功修改了,id:" + students.getId());
			} else
			{
				return;
			}
		} catch (NumberFormatException e)
		{
			System.out.println("你输入的不是整数,请重新输入");
			this.updateOneStudent(br);
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * 添加一个学生
	 */
	private void saveOneStudent(BufferedReader br)
	{
		System.out.println("请依次输入学生的相关信息:姓名,性别(0/1),生日,毕业院校");
		System.out.println("示例:张三;0;1996-12-03;郑大");
		try
		{
			String line = br.readLine();
			line = line.trim();
			String[] lines = line.split(";");
			/* 判断性别 */
			if (lines.length != 4)
			{
				System.out.println("你输入的格式有问题");
				return;
			}
			Student student = new Student();
			if (student.isSex())
			{
				line = "0";
			} else
			{
				line = "1";
			}
			student.setName(lines[0]);
			/* 性输入 0 是男 1 是女 */
			if ("0".equalsIgnoreCase(lines[1]))
			{
				student.setSex(true);
			} else if ("1".equalsIgnoreCase(lines[1]))
			{
				student.setSex(false);
			} else
			{
				/* 其他输入都为错误 */
				System.out.println("=输入有误请按照格式填写=");
				return;
			}
			// 生日,毕业院校,分数
			student.setAge(lines[2]);
			student.setSchool(lines[3]);
			student.setUpdateTime(new Date());
			student.setCreationTime(new Date());
			int res = this.usersService.saveOneStudentService(student);
			System.out.println("添加成功,id:" + res);
		} catch (NumberFormatException e)
		{
			System.out.println("你输入的不是整数,请重新输入");
			this.saveOneStudent(br);
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * 查询所有学生
	 */
	private void selectListStudent()
	{
		List<Student> list = this.usersService.selectListStudentService();
		System.out.println(this.usersService.printTitleStudentService());
		for (Student students : list)
		{
			System.out.println(students);
		}

	}

	public void setDemoService(IDemoService demoService)
	{
		this.demoService = demoService;
	}

	public void setQuestionService(IQuestionService questionService)
	{
		this.questionService = questionService;
	}

	public void setUsersService(IUsersService usersService)
	{
		this.usersService = usersService;
	}
}